﻿
function sayHello(){
    alert("hello from ExtendScript");
}
var dodo = function (info)
{
  //var obj = eval ('('+info +')')
  //alert(obj.name)
    //alert(info);// 弹出信息
    //alert(info1);// 弹出信息
}
var dodo1 = function ()
{
  alert("dodo:");// 弹出信息
}
function getJson()
{
   alert("jj");// 弹出信息
   var json = new File("/C/Users/MYL/Desktop/qq.json")
   if(json.exists)
    {
     var jj;
     json.open();
     jj=json.read();
     var jstring = JSON.stringify(jj);
     alert(jstring);// 弹出信息
     var jobj =JSON.parse(jstring);
     alert(jobj.Spatial_Ref);// 弹出信息
     //var fontlist = new Object(); // 创建一个要传递的对象
     return JSON.stringify(jj); // 把对象转化成 JSON 字符串并返回
        
    }
}

function group(array, subGroupLength) {
      var index = 0;
      var newArray = [];
      while(index < array.length) {
          newArray.push(array.slice(index, index += subGroupLength));
      }
      return newArray;
}
/* 要素绘制函数，需传入点坐标数组pts*/
function addLine(pts)
{       
    var str =pts.toString();
    var pt_line = str.split(",");//得到点数组
    var lineList = new Array();//绘制数组
    var arrayx = new Array();
    var arrayy = new Array();//用于排序得到四至
    for(var i=0;i<pt_line.length;)
    {
        arrayx[i/2]=parseFloat(pt_line[i]);
        arrayy[i/2]=parseFloat(pt_line[i+1]);
        i=i+2;   
    }
    arrayx.sort(function (a, b) {
                   return a-b;
                 });
    arrayy.sort(function (a, b) {
                   return a-b;
                 })
    var w = arrayx[arrayx.length-1]-arrayx[0];
    var h = arrayy[arrayy.length-1]-arrayy[0];
    var xscale = (app.activeDocument.cropBox[2] -app.activeDocument.cropBox[1])/w;
    var yscale = (app.activeDocument.cropBox[0] -app.activeDocument.cropBox[3])/h;
    alert(xscale);alert(yscale);
    for(var i=0;i<pt_line.length;)
    {   
        var newx;
        var newy;
        if(xscale>yscale)
        {
          newy = yscale*(parseFloat(pt_line[i+1])-arrayy[arrayy.length-1]);
          newx = yscale*(parseFloat(pt_line[i])-arrayx[0]);
        }
        else 
        {
          newy = xscale*(parseFloat(pt_line[i+1])-arrayy[arrayy.length-1]);
          newx = xscale*(parseFloat(pt_line[i])-arrayx[0]);
        }
        //alert(newx);
        lineList[i/2] = new Array(newx,newy);
        //alert(lineList[i]);
        i=i+2;
    }//坐标变换
    //lineList[pt_line.length/2-1]=lineList[0];
    if ( app.documents.length > 0 ) {
    alert(lineList.length);
    app.defaultStroked = true;
    var lineGroup = app.activeDocument.groupItems.add();
    newPath = lineGroup.pathItems.add(); 
    newPath.setEntirePath(lineList);//绘制要素
    }
}
function MultiLine(pts)
{
    var lineGroup = app.activeDocument.groupItems.add();
    var str =pts.toString();
    var pt_line = str.split(",");//得到点数组
    var lineList = new Array();//绘制数组
    var arrayx = new Array();
    var arrayy = new Array();//用于排序得到四至
    for(var i=0;i<pt_line.length;)
    {
        arrayx[parseInt(i/2)]=parseFloat(pt_line[i]);
        arrayy[parseInt(i/2)]=parseFloat(pt_line[i+1]);
        i=i+2;   
    }
    arrayx.sort(function (a, b) {
                   return a-b;
                 });
    arrayy.sort(function (a, b) {
                   return a-b;
                 })
    var w = arrayx[arrayx.length-1]-arrayx[0];
    var h = arrayy[arrayy.length-1]-arrayy[0];
    var xscale = (app.activeDocument.cropBox[2] -app.activeDocument.cropBox[1])/w;
    var yscale = (app.activeDocument.cropBox[0] -app.activeDocument.cropBox[3])/h;
   if ( app.documents.length > 0 ) {
    //var wkt = "POLYGON((100 100,500 100,500 500,100 500,100 100))";  
    //var test = new Array();  
    //test = wkt.substring(wkt.indexOf('(')+2,wkt.length-2).split(","); 
    var length =pt_line.length;
    var flag= parseInt(length /2000);
    alert(length);
    var pos = 0;
    alert(flag)
    if(flag>0)
    {
       for(var j=0 ; j< flag;j++)
       {
          var lineList = new Array();
          if(j!==0)
          pos = pos -2;  
          //alert(pos);
          for(var k =0 ; k< 1000 ;k++)
          {
            var newx;
            var newy;
            if(xscale>yscale)
            {
              newy = yscale*(parseFloat(pt_line[pos+1])-arrayy[arrayy.length-1]);
              newx = yscale*(parseFloat(pt_line[pos])-arrayx[0]);
            }
            else 
            {
              newy = xscale*(parseFloat(pt_line[pos+1])-arrayy[arrayy.length-1]);
              newx = xscale*(parseFloat(pt_line[pos])-arrayx[0]);
            }
            //alert(newx);
            //lineList[pos/2] = new Array(newx,newy);
            //alert(lineList[i]);
            pos=pos+2;
            lineList[k] = new Array(newx,newy);
          }
          app.defaultStroked = false;
          newPath = lineGroup.pathItems.add(); 
          newPath.filled =false;
          newPath.setEntirePath(lineList);
          //alert(j)
       }
       var rem = length %1998 - 2;
       var lineList = new Array();
       pos = pos -2;
       //pos = length -rem + 2;
       alert(rem);
        for(var k =0 ; k< parseInt(rem/2) ;k++)
       {      
        var newx;
        var newy;
        if(xscale>yscale)
        {
          newy = yscale*(parseFloat(pt_line[pos+1])-arrayy[arrayy.length-1]);
          newx = yscale*(parseFloat(pt_line[pos])-arrayx[0]);
        }
        else 
        {
          newy = xscale*(parseFloat(pt_line[pos+1])-arrayy[arrayy.length-1]);
          newx = xscale*(parseFloat(pt_line[pos])-arrayx[0]);
        }
        //alert(newx);
        //lineList[pos/2] = new Array(newx,newy);
        //alert(lineList[i]);
        pos = pos+2;
        lineList[k] = new Array(newx,newy);
       }
       app.defaultStroked = false;
       newPath = lineGroup.pathItems.add(); 
       newPath.filled =false;
       newPath.setEntirePath(lineList);
       
    } 
   else{
         var lineList = new Array();
         for(var k =0 ; k< length ;k++)
        {
            var newx;
            var newy;
            if(xscale>yscale)
            {
              newy = yscale*(parseFloat(pt_line[pos+1])-arrayy[arrayy.length-1]);
              newx = yscale*(parseFloat(pt_line[pos])-arrayx[0]);
            }
            else 
            {
              newy = xscale*(parseFloat(pt_line[pos+1])-arrayy[arrayy.length-1]);
              newx = xscale*(parseFloat(pt_line[pos])-arrayx[0]);
            }
            pos=pos+2;
            lineList[k] = new Array(newx,newy);
        }
        alert("00")
        newPath = lineGroup.pathItems.add(); 
        newPath.setEntirePath(lineList);
  }
}
}

function MultiLine1(pts,ptBounds,i,layer_name,label,properties)
{
    //alert("000");
    var lineGroup;
    var layer;
    var docRef = app.activeDocument; 
    if(i ==0)
    {
      layer = docRef.layers.add();
      layer.name = layer_name;
    }
    else
        layer = docRef.layers[0];
    lineGroup = layer.groupItems.add();
    lineGroup.name = label;
    //app.activeDocument.layers[0].name = layer_name;
    var str =pts.toString();
    var pt_line = str.split(",");//得到点数组
    var lineList = new Array();//绘制数组
    var arrayx = new Array();
    var arrayy = new Array();//用于排序得到四至
    var array_bounds = new Array();//用于排序得到四至
    var boundstr = ptBounds.toString();
    var boundpt = boundstr.split(",");//得到四至数组
    //alert("111");
    for(var i=0;i<pt_line.length;)
    {
        arrayx[parseInt(i/2)]=parseFloat(pt_line[i]);
        arrayy[parseInt(i/2)]=parseFloat(pt_line[i+1]);
        i=i+2;   
    }
    for(var i=0;i<boundpt.length;i++)
    {
        array_bounds[i] = parseFloat(boundpt[i]);
    }
    /*
    arrayx.sort(function (a, b) {
                   return a-b;
                 });
    arrayy.sort(function (a, b) {
                   return a-b;
                 })
    */
    var w = array_bounds[2]-array_bounds[0];
    var h = array_bounds[1] - array_bounds[3];
    var xscale = (app.activeDocument.cropBox[2] -app.activeDocument.cropBox[1])/w;
    var yscale = (app.activeDocument.cropBox[0] -app.activeDocument.cropBox[3])/h;
    if ( app.documents.length > 0 ) {
    //var wkt = "POLYGON((100 100,500 100,500 500,100 500,100 100))";  
    //var test = new Array();  
    //test = wkt.substring(wkt.indexOf('(')+2,wkt.length-2).split(","); 
    var length =pt_line.length;
    var pos=0;
    if(length > 2000)
    {
        for(var k =0 ; k< 1000 ;k++)
        {
            var newx;
            var newy;
            if(xscale>yscale)
            {
              newy = yscale*(parseFloat(pt_line[pos+1])-array_bounds[1]);
              newx = yscale*(parseFloat(pt_line[pos])-array_bounds[0]);
            }
            else 
            {
              newy = xscale*(parseFloat(pt_line[pos+1])-array_bounds[1]);
              newx = xscale*(parseFloat(pt_line[pos])-array_bounds[0]);
            }
            pos=pos+2;
            lineList[k] = new Array(newx,newy);
          }
        app.defaultStroked = false;
        newPath = lineGroup.pathItems.add(); 
        newPath.filled =false;
        newPath.setEntirePath(lineList);
        for(var k = 1000; k<length/2;k++)
        {
            //var newPoint = app.activeDocument.pathItems[app.activeDocument.pathItems.length -1].pathPoints.add();
            var newPoint = newPath.pathPoints.add();
            var newx;
            var newy;
           if(xscale>yscale)
            {
              newy = yscale*(parseFloat(pt_line[pos+1])-array_bounds[1]);
              newx = yscale*(parseFloat(pt_line[pos])-array_bounds[0]);
            }
            else 
            {
              newy = xscale*(parseFloat(pt_line[pos+1])-array_bounds[1]);
              newx = xscale*(parseFloat(pt_line[pos])-array_bounds[0]);
            }
            pos=pos+2;
            newPoint.anchor = Array(newx, newy); 
            newPoint.leftDirection = newPoint.anchor; 
            newPoint.rightDirection = newPoint.anchor; 
            newPoint.pointType = PointType.CORNER;
        }
    }
    else{
        for(var k = 0; k<length/2;k++)
        {
            var newx;
            var newy;
            if(xscale>yscale)
            {
              newy = yscale*(parseFloat(pt_line[pos+1])-array_bounds[1]);
              newx = yscale*(parseFloat(pt_line[pos])-array_bounds[0]);
            }
            else 
            {
              newy = xscale*(parseFloat(pt_line[pos+1])-array_bounds[1]);
              newx = xscale*(parseFloat(pt_line[pos])-array_bounds[0]);
            }
            pos=pos+2;
            lineList[k] = new Array(newx,newy);
        }
        app.defaultStroked = false;
        newPath = lineGroup.pathItems.add(); 
        newPath.filled =false;
        newPath.setEntirePath(lineList);
    }
    newPath.close = true;
    // Define the new color
    var newRGBColor = new RGBColor();
    newRGBColor.red = Math.random()*255;
    newRGBColor.green = Math.random()*255;
    newRGBColor.blue = 0; 
    //newPath.fillColor = newRGBColor;
    //newPath.note = properties.toString();
   
    //alert(newPath.note);
}
}
function getCanvasSize(){
  return app.activeDocument.cropBox
}
function drawPointFeatures(features,test){
  test =  "XXX"
  return test
  //alert(feature_objs)
  /*
  var feature_objs = eval('(' + features + ')');
  var docRef = app.activeDocument; 
  var layer = docRef.layers.add();
  layer.name = layer_name;
  for (var i = 0; i<feature_objs.length;i++){

  }*/
}
function AddPoints(pts,layer_name,ptBounds)
{
    alert("draw_point_layer");
    for(var jj = 0; jj < pts.length;jj++)
        alert(pts[jj])
    //alert(pts)
    //alert(pts[0].x)
    var str =pts.toString();
    //alert(str)
    var pt_array = str.split(",");//得到点数组
    var docRef = app.activeDocument; 
    var layer = docRef.layers.add();
    layer.name = layer_name;
    //app.activeDocument.layers[0].name = layer_name;
    var iCount = docRef.symbols.length; //在符号库寻找对应符号
    //alert(iCount);
    var array_bounds = new Array();//用于排序得到四至
    var boundstr = ptBounds.toString();
    var boundpt = boundstr.split(",");//得到四至数组
    for(var i=0;i<boundpt.length;i++)
    {
        array_bounds[i] = parseFloat(boundpt[i]);
    }

    /*
    for(var i=0; i<iCount; i++) {
        //if(name ===docRef.symbols[i].name)
        symbolRef = docRef.symbols[0];
        if(i==15)
        {
            symbolRef = docRef.symbols[i];
            alert(symbolRef.name);
            break;
        }
        //alert(docRef.symbols)
        if(i== iCount-1)
            alert("未匹配到对应符号，请检查符号库");
    }*/
    symbolRef = docRef.symbols[iCount-1];
    //alert(pt_array.length);
    var pos = 0;
    
    var w = array_bounds[2]-array_bounds[0];
    var h = array_bounds[1] - array_bounds[3];
    var xscale = (app.activeDocument.cropBox[2] -app.activeDocument.cropBox[1])/w;
    var yscale = (app.activeDocument.cropBox[0] -app.activeDocument.cropBox[3])/h;
    
    
    for(var i = 0 ; i<pt_array.length/2;i++)
    {
        //var y = parseFloat(pt_array[i+1]);
        //var x = parseFloat(pt_array[i]);
        var newx;
        var newy;
        if(xscale>yscale)
        {
          newy = yscale*(parseFloat(pt_array[pos+1])-array_bounds[1]);
          newx = yscale*(parseFloat(pt_array[pos])-array_bounds[0]);
        }
        else 
        {
          newy = xscale*(parseFloat(pt_array[pos+1])-array_bounds[1]);
          newx = xscale*(parseFloat(pt_array[pos])-array_bounds[0]);
        }
        pos = pos + 2;
        symbolItemRef1 = layer.symbolItems.add(symbolRef); 
        //symbolItemRef1 = docRef.symbolItems.add(symbolRef); 
        symbolItemRef1.height = 0.1*symbolItemRef1.height;
        symbolItemRef1.width = 0.1*symbolItemRef1.width;
        symbolItemRef1.top = newy -0.5*symbolItemRef1.height;
        symbolItemRef1.left = newx + 0.5*symbolItemRef1.width;
    }
}
//绘制单个面、线要素
function drawPoly(feature_str){
  var docRef = app.activeDocument; 
  layer = docRef.layers[0];
  
  var feature = eval ('('+feature_str +')')
  //alert(feature.feature_type)
  var geometry = feature.geometry;
  //alert(geometry[0].x)
  
  var lineGroup = layer.groupItems.add();
  //lineGroup.name = label;
  //var pts = str_pts.split(',');
  var length =geometry.length;
  var pts_draw =  new Array;
  var pos=0;
  if(length > 1000)
  {
      for(var k =0 ; k< 1000 ;k++)
      {
          pts_draw[k] = new Array(geometry[k].screen_x,geometry[k].screen_y);
          pos=pos+2;
      }
      app.defaultStroked = false;
      newPath = lineGroup.pathItems.add(); 
      newPath.filled =false;
      newPath.setEntirePath(pts_draw);
      for(var k = 1000; k<length;k++)
      {
          //var newPoint = app.activeDocument.pathItems[app.activeDocument.pathItems.length -1].pathPoints.add();
          var newPoint = newPath.pathPoints.add();
          pos=pos+2;
          newPoint.anchor = Array(geometry[k].screen_x,geometry[k].screen_y); 
          newPoint.leftDirection = newPoint.anchor; 
          newPoint.rightDirection = newPoint.anchor; 
          newPoint.pointType = PointType.CORNER;
      }
  }
  else{
      for(var k = 0; k<length;k++)
      {
        pts_draw[k] = new Array(geometry[k].screen_x,geometry[k].screen_y);
      }
      app.defaultStroked = false;
      newPath = lineGroup.pathItems.add(); 
      newPath.filled =false;
      newPath.setEntirePath(pts_draw);
  }
  newPath.close = true;
  // Define the new color
  //var newRGBColor = new RGBColor();
  //newRGBColor.red = Math.random()*255;
  //newRGBColor.green = Math.random()*255;
  //newRGBColor.blue = 0; 
  //newPath.fillColor = newRGBColor;
  //newPath.note = properties.toString();*/
}
function drawFeature(feature){
  switch (feature.feature_type) {
    case "esriGeometryPolygon":
    {
      drawPoly(feature);
      break;
    }
        
    case 1:
        day = "星期一";
        break;
    case 2:
        day = "星期二";
        break;
    case 3:
        day = "星期三";
        break;
    case 4:
        day = "星期四";
        break;
    case 5:
        day = "星期五";
        break;
    default:
        day = "星期六";
  }
}

function addLayer(layer_name)
{
    alert("add Layer  " + layer_name);
    //var layer_objs = eval('(' + feature_layer + ')');
    var layer;
    var docRef = app.activeDocument; 
    layer = docRef.layers.add();
    layer.name = layer_name;
}

function drawPoint(feature_str){
  var feature = eval ('('+feature_str +')')
  var docRef = app.activeDocument; 
  var symbolRef = docRef.symbols.getByName("公园");
  var layer = docRef.layers[0];
  //alert(layer.name)
  for(var i = 0;i < feature.geometry.length;i++){
    var pt = feature.geometry[i]
    //alert(pt)
    symbolItemRef1 = layer.symbolItems.add(symbolRef); 
    //symbolItemRef1 = docRef.symbolItems.add(symbolRef); 
    symbolItemRef1.height = symbolItemRef1.height;
    symbolItemRef1.width = symbolItemRef1.width;
    symbolItemRef1.top = pt.screen_y;
    symbolItemRef1.left = pt.screen_x;
  }
  
}
